<?

include_class('general_media');
include_class('m2');
include_class('band_members');

$topURL = (USE_MOD_REWRITE) ? SITE_WEB_DIRECTORY . '/gm/' : SITE_WEB_DIRECTORY . '/media.php';

$id = ($_GET['id'] > 0 && is_numeric($_GET['id'])) ? $_GET['id'] : 0;
if ($id != 0) {
	$ma = MediaArea::get($id);
	if (!$ma->isGeneralMedia()) {
		header('Location: media.php');
		exit;
	}
	$areaList = $ma->getGeneralAreas(1);
	$title = $ma->getTitle();
	if ($_GET['media_instance_id'] > 0) { 
		$mi = MediaInstance::get($_GET['media_instance_id']);
		if (!db::isError($mi) && !db::isError($ma)) {
			if ($mi->getAreaID() == $ma->getID()) {
				$view = 'detail';
				$detail = new SectionTemplate;
				$detail->file = "media_detail.php";
				$detail->args['media'] = $mi;
				$detail->args['prev'] = $mi->getPrevious();
				$detail->args['next'] = $mi->getNext();
				$detail->args['exitURL'] = (USE_MOD_REWRITE) ? '/gm/' . $ma->getID() . '/' : '/media.php?id=' . $ma->getID();
			}
		}
	}
	
	$breadcrumb = array();
	$trail = $ma->getAreaTrail();
	if ($trail) {
		foreach($trail as $maBC) {
			$mediaArea = new SectionTemplate;
			$mediaArea->file = "media_breadcrumb.php";
			$mediaArea->args['activeArea'] = $ma;
			$mediaArea->args['area'] = $maBC;
			$breadcrumb[] = $mediaArea;
		}
	} else {
		unset($breadcrumb);
	}
		
}

if (!$ma) {
	$areaList = MediaArea::getGeneralAreas(1);
	$title = "Rich Media";
} else {
	$media = new SectionTemplate;
	$media->file = ($ma->getDisplay() == 'grid') ? "gallery.php" : "media_list.php";
	$media->args['area'] = &$ma;
	if (USE_MOD_REWRITE) {
		$media->args['url'] = '/gm/' . $ma->getID() . '/';
	} else {
		$media->args['url'] = SITE_WEB_DIRECTORY . '/media.php?id=' . $ma->getID();
	}
}

$areas = array();
if (count($areaList) > 0) {
	foreach($areaList as $a) {
		$areaT = new SectionTemplate;
		$areaT->file = "media_area.php";
		$areaT->args['area'] = $a;
		$areas[] = $areaT;
	}
} else {
	unset($areas);
}



$header = new SectionTemplate;
$header->file = "header.php";
$header->args['title'] = $title;

$footer = new SectionTemplate;
$footer->file = "footer.php";

?>



